package com.stillcoolme.basic.utils;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public class JackSonUtils {
    // 用户类示例
    @Data
    @AllArgsConstructor
    @JsonInclude(JsonInclude.Include.NON_NULL) // 忽略null字段
    static class User {
        @JsonProperty("user_id") // 自定义JSON字段名
        private String userId;
        private String username;
        private LocalDateTime createTime;

        // 构造方法、Getter和Setter略
    }

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static String toJson(Object obj) {
        String objectString = null;
        try {
            objectString = objectMapper.writeValueAsString(obj);
            return objectString;
        } catch (JsonProcessingException e) {
            throw new RuntimeException("convert json fail", e);
        }
    }

    public static Object toObject(String jsonString,  Class<?> clazz) {
        try {
            return objectMapper.readValue(jsonString, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("convert json fail", e);
        }
    }

    // JSON字符串转List
    public static <T> List<T> toList(String json, Class<T> elementClass) {
        try {
            return objectMapper.readValue(json,
                    objectMapper.getTypeFactory().constructCollectionType(List.class, elementClass));
        } catch (Exception e) {
            throw new RuntimeException("JSON转List失败", e);
        }
    }

    // JSON字符串转复杂对象（如Map嵌套）
    public static <T> T toComplexObject(String json, TypeReference<T> typeReference) {
        try {
            return objectMapper.readValue(json, typeReference);
        } catch (Exception e) {
            throw new RuntimeException("JSON转复杂对象失败", e);
        }
    }


    public static void main(String[] args) {
        // 使用示例
        User user = new User("1001", "john_doe", LocalDateTime.now());
        String json = toJson(user);
        User parsedUser = (User) toObject(json, User.class);

        // 使用示例
        String jsonList = "[{\"productId\":\"P001\",\"name\":\"iPhone\",\"price\":9999.0}]";
        List<Product> productList = toList(jsonList, Product.class);

        String jsonComplexMap = "{\"order\": [{\"productId\":\"P001\",\"name\":\"iPhone\",\"price\":9999.0}]}";

        // 处理复杂嵌套结构,要先 new TypeReference
        TypeReference<Map<String, List<Order>>> typeRef =
                new TypeReference<Map<String, List<Order>>>() {};
        Map<String, List<Order>> resultMap = toComplexObject(jsonComplexMap, typeRef);

    }



    // 订单类示例
    class Order {
        private String orderId;
        private List<Product> products;
        private Map<String, Object> extraInfo;

        // 构造方法、Getter和Setter略
    }

    // 商品类示例
    class Product {
        private String productId;
        private String name;
        private double price;

        // 构造方法、Getter和Setter略
    }

}
